<template>
  <view>
    <view class="goodsitem" @click="goDeatil(item.goods_id)" v-for="item,index in goodslist" :key="item.goods_id">
      <my-goods :goods="item"></my-goods>
    </view>
    <view class="isAll" v-if="isAll">没有更多了...</view>
  </view>
</template>

<script>
  import {
    getSearchGoods
  } from '@/api/goods.js'
  export default {
    data() {
      return {
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        goodslist: [],
        total: '',
        isAll: false,
        isLoading: false
      };
    },
    methods: {
      async AgetSearchGoods(cb) {
        this.isLoading = true
        try {
          const res = await getSearchGoods(this.queryObj)
          console.log('进入页面', res);
          this.goodslist = this.goodslist.concat(res.message.goods)
          this.total = res.message.total
        } finally {
          this.isLoading = false
          cb && cb()
        }
      },
      goDeatil(id) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
        })
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      console.log('请求参数', this.queryObj);
      this.AgetSearchGoods()
    },
    onPullDownRefresh() {
      this.goodslist = [],
        this.queryObj.pagenum = 1,
        this.total = '',
        this.isAll = false,
        this.isLoading = false
      // this.toast.await('刷新完成', 'success')
      this.AgetSearchGoods(function() {
        uni.stopPullDownRefresh()
      })

    },
    onReachBottom() {
      if (this.isLoading) return
      if (this.queryObj.pagenum * this.queryObj.pagesize < this.total) {
        this.queryObj.pagenum += 1
        this.AgetSearchGoods()
      } else {
        this.isAll = true
        this.toast('没有更多了...', 'none')
      }
    }
  }
</script>

<style lang="scss">

</style>
